//
//  BankDetailsCell.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/9/25.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class BankDetailsCell: UITableViewCell {
    
    fileprivate let bankImageView = UIImageView()
    fileprivate let nameLabel = UILabel() //银行名称
    fileprivate let addressLabel = UILabel() //地址
    fileprivate let distanceLabel = UILabel() //距离
    
    var model : BankDetailsModel! {
        didSet{
            getData()
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        bankImageView.image = UIImage.imageWithName("loading")
        self.contentView.addSubview(bankImageView)
        
        nameLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        self.contentView.addSubview(nameLabel)
        
        addressLabel.textColor = UIColor.gray
        addressLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        self.contentView.addSubview(addressLabel)
        
        distanceLabel.textColor = UIColor.blue
        distanceLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        distanceLabel.textAlignment = .right
        self.contentView.addSubview(distanceLabel)
        
        let lineView = UIView()
        lineView.backgroundColor = UIColor.lineColor()
        self.contentView.addSubview(lineView)
        
        bankImageView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(RATIO(maxNum: 10))
            make.width.height.equalTo(RATIO_H(maxNum: 40))
        }
        
        distanceLabel.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(RATIO(maxNum: -10))
            make.width.greaterThanOrEqualTo(50)
            make.height.greaterThanOrEqualTo(10)
            make.centerY.equalToSuperview()
        }
        
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(bankImageView.snp.right).offset(RATIO(maxNum: 2))
            make.right.equalTo(distanceLabel.snp.left).offset(RATIO(maxNum: -2))
            make.height.greaterThanOrEqualTo(10)
            make.top.equalTo(bankImageView)
        }
        
        addressLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.right.equalTo(nameLabel)
            make.height.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(RATIO_H(maxNum: 10))
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
    }
    
    //MARK: 填充数据
    fileprivate func getData() {
        
        nameLabel.text = model.name
        addressLabel.text = model.address
        
        if model.distance! > 1000 {
            let km = "\(Float(model.distance!)/1000)"
            distanceLabel.text = km + "km"
        }else{
            distanceLabel.text = "\(model.distance!)" + "m"
        }
        
        let image = KMTool.selectBankImage(bankName: model.name!)
        bankImageView.image = UIImage.imageWithName(image)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

    }

}
